package com.hcxdi.app.droidbase;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.hcxdi.api.bean.auth.Auth;
import com.hcxdi.api.bean.auth.AuthReq;
import com.hcxdi.app.droidbase.bean.Order;
import com.hcxdi.app.droidbase.bean.OrderLog;
import com.hcxdi.app.droidbase.contract.AuthContract;
import com.hcxdi.app.droidbase.presenter.AuthPresenter;
import rx.Observable;
import rx.Subscriber;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;

/*
            Presenter	Activity	Fragment	View
        onViewAttached	onStart	    onResume	onAttachedToWindow
        onViewDetached	onStop	    onPause	    onDetachedFromWindow
        onDestroyed	    onDestroy	onDestroy	onDetachedFromWindow
*/

public class MainActivity extends BaseOnActivity  implements AuthContract.View{

    AuthPresenter mAuthPresenter;

    String TAG="MainActivity";

    TextView textView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mAuthPresenter= new AuthPresenter();


        textView=(TextView)findViewById(R.id.textView);
        Order o1= new Order();
        Order o2= new Order();
        OrderLog[] ol1= new OrderLog[]{new OrderLog(),new OrderLog()};
        OrderLog[] ol2= new OrderLog[]{new OrderLog(),new OrderLog(),new OrderLog()};
        o1.name="o1";
        o2.name="o2";
        o1.orderLogs=ol1;
        //o2.orderLogs=ol2;

        Order[] _order = new Order[]{o1,o2};
        //map(_order);
        /*Log.e(TAG,"1--------------");
        hello("a","b","c");
        Log.e(TAG,"2--------------");*/
        Button loginButton=(Button) findViewById(R.id.login);
        loginButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                login();
            }
        });
    }


    private void hello(String...hello){
        Observable<String> _observable= Observable.from(hello)
        .subscribeOn(Schedulers.immediate())
        .observeOn(AndroidSchedulers.mainThread());

        _observable.subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.e(TAG,"3--------------");
                textView.setText(s);
            }
        });


         Observable.just("ef")
                .observeOn(AndroidSchedulers.mainThread())
                .subscribeOn(Schedulers.immediate())
                .subscribe(new Subscriber<String>() {


                    @Override
                    public void onStart() {
                        super.onStart();
                        //showdialog
                        Log.e(TAG,"4--------------onStart");
                    }

                    @Override
                    public void onCompleted() {
                        //closedialog
                        Log.e(TAG,"4--------------onCompleted");
                    }

                    @Override
                    public void onError(Throwable e) {
                        //closedialog
                        Log.e(TAG,"4--------------onError");
                    }

                    @Override
                    public void onNext(String s) {
                        Log.e(TAG,"4--------------onNext");
                    }
                });


    }


    private void map(Order...ordes){
        Observable.from(ordes).map(new Func1<Order, String>() {
            @Override
            public String call(Order order) {
                return order.name;
            }
        }).subscribe(new Action1<String>() {
            @Override
            public void call(String s) {
                Log.e(TAG,"order name ="+s);
            }
        });

        Observable.from(ordes)
                .filter(new Func1<Order, Boolean>() {
                    @Override
                    public Boolean call(Order order) {
                        return order.orderLogs!=null;
                    }
                })
                .flatMap(new Func1<Order, Observable<OrderLog>>() {
                    @Override
                    public Observable<OrderLog> call(Order order) {
                        return Observable.from(order.orderLogs);
                    }
                })
               /* .filter(new Func1<OrderLog, Boolean>() {
                    @Override
                    public Boolean call(OrderLog orderLog) {
                        return orderLog!=null;
                    }
                })*/
                .subscribe(new Action1<OrderLog>() {
                    @Override
                    public void call(OrderLog orderLog) {
                        Log.e(TAG,"orderLog");
                    }
                });
    }


    @Override
    protected void onStart() {
        super.onStart();
        mAuthPresenter.onViewAttached(this);
    }


    @Override
    protected void onStop() {
        super.onStop();
        mAuthPresenter.onViewDetached();
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        mAuthPresenter.onDestroyed();
    }


    private void login(){
        Log.e(TAG,"login...........");
        AuthReq authReq = new AuthReq("15108280907","123456",1,0);
        mAuthPresenter.loadAuth(authReq);
    }

    @Override
    public void onAuthed(Auth auth) {
        Log.e(TAG,"onAuthed...........");
        Toast.makeText(this, "success "+auth.getId(), Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onError(Throwable msg) {
        Log.e(TAG,"onError...........");
        Toast.makeText(this, "error "+msg, Toast.LENGTH_SHORT).show();
    }


}
